<template>
  <view class="container">
    <view class="background-wrapper">
      <image class="background" src="/static/login.jpg" mode="aspectFill"></image>
      <view class="overlay"></view>
    </view>

    <view class="content">
      <view class="login-card">
        <view class="header">
          <text class="title">智A食品商城</text>
          <text class="subtitle">欢迎登录</text>
        </view>
        
        <view class="form">
          <view class="input-group">
            <text class="label">账号：</text>
            <input 
              type="text" 
              placeholder="请输入账号/手机号" 
              v-model="username" 
              class="input-field"
              placeholder-class="placeholder" 
            />
          </view>
          
          <view class="input-group">
            <text class="label">密码：</text>
            <input 
              type="password" 
              placeholder="请输入密码" 
              v-model="password" 
              class="input-field"
              placeholder-class="placeholder" 
            />
          </view>
          
          <view class="options">
            <view class="remember-pwd" @tap="toggleRemember">
              <view class="checkbox" :class="{ 'checked': rememberPassword }">
                <view class="checkbox-inner" v-if="rememberPassword">✔</view>
              </view>
              <text>记住密码</text>
            </view>
            <text @tap="goForgotPwd" class="forgot-pwd">忘记密码</text>
          </view>
          
          <button @tap="login" class="login-btn" hover-class="button-hover">
            <text>登录</text>
          </button>
        </view>
        
        <view class="links">
          <text @tap="goRegister" class="link-text">没有账号？去注册</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { userApi } from '@/api/index';

export default {
  data() {
    return {
      username: '',
      password: '',
      rememberPassword: false
    }
  },
  onLoad() {
    // 从本地缓存中读取用户名和密码
    try {
      const loginInfo = uni.getStorageSync('loginInfo');
      if (loginInfo) {
        const { username, password, rememberPassword } = JSON.parse(loginInfo);
        if (rememberPassword) {
          this.username = username;
          this.password = password;
          this.rememberPassword = true;
        }
      }
    } catch (e) {
      console.error('读取存储失败:', e);
    }
  },
  methods: {
    toggleRemember() {
      this.rememberPassword = !this.rememberPassword;
    },
    login() {
      // 输入验证
      if (!this.username.trim()) {
        uni.showToast({
          title: '请输入账号/手机号',
          icon: 'none'
        });
        return;
      }
      
      if (!this.password) {
        uni.showToast({
          title: '请输入密码',
          icon: 'none'
        });
        return;
      }
      
      // 记录存储的用户名密码以便调试
      console.log("============================");
      console.log("存储的用户名:", this.username);
      console.log("当前输入用户名:", this.username);
      console.log("存储的密码:", this.password);
      console.log("当前输入密码:", this.password);
      console.log("============================");
      
      // 登录请求参数
      const params = {
        username: this.username,
        password: this.password
      };
      
      console.log("登录请求参数:", params);
      
      // 显示加载状态
      uni.showLoading({
        title: '登录中...'
      });
      
      // 使用API接口登录
      uni.request({
        url: 'https://m1.apifoxmock.com/m1/6102433-5793108-default/admin/login',
        method: 'POST',
        data: params,
        header: {
          'Content-Type': 'application/json'
        },
        success: (res) => {
          console.log("登录响应:", res);
          this.handleLoginSuccess(res);
        },
        fail: (err) => {
          console.error("登录失败:", err);
          this.handleLoginFail(err);
        },
        complete: () => {
          uni.hideLoading();
        }
      });
    },
    
    handleLoginSuccess(res) {
      if (res.statusCode === 200 && (res.data.code === 0 || res.data.code === 200)) {
        console.log("登录成功:", res.data);
        
        // 保存登录状态
        uni.setStorageSync('token', res.data.token || '');
        uni.setStorageSync('userInfo', JSON.stringify(res.data.userInfo || {}));
        
        // 记住密码
        if (this.rememberPassword) {
          try {
            uni.setStorageSync('loginInfo', JSON.stringify({
              username: this.username,
              password: this.password,
              rememberPassword: true
            }));
          } catch (e) {
            console.error('保存登录信息失败:', e);
          }
        } else {
          // 如果不记住密码，则清除之前保存的登录信息
          uni.removeStorageSync('loginInfo');
        }
        
        // 显示成功提示
        uni.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 1500,
          success: () => {
            // 跳转到首页
            setTimeout(() => {
              uni.switchTab({
                url: '/pages/index/index'
              });
            }, 1500);
          }
        });
      } else {
        // 登录失败
        console.error("登录响应错误:", res.data);
        uni.showToast({
          title: res.data.msg || '登录失败，请检查账号密码',
          icon: 'none'
        });
      }
    },
    
    handleLoginFail(err) {
      console.error("请求错误:", err);
      uni.showToast({
        title: '网络错误，请稍后重试',
        icon: 'none'
      });
    },
    
    goRegister() {
      uni.navigateTo({
        url: './register'
      });
    },
    
    goForgotPwd() {
      uni.navigateTo({
        url: './forgot'
      });
    }
  }
}
</script>

<style lang="scss">
.container {
  position: relative;
  height: 100vh;
  width: 100%;
}

.background-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.background {
  width: 100%;
  height: 100%;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.4),
    rgba(0, 0, 0, 0.7)
  );
}

.content {
  position: relative;
  z-index: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 20px;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 40px 30px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.header {
  text-align: center;
  margin-bottom: 40px;
}

.title {
  font-size: 32px;
  font-weight: 600;
  color: #333;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 16px;
  color: #666;
}

.form {
  margin-bottom: 30px;
}

.input-group {
  background: #f5f5f5;
  border-radius: 12px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  padding: 4px 16px;
  transition: all 0.3s ease;
  
  &:focus-within {
    background: #fff;
    box-shadow: 0 0 0 2px rgba(255, 81, 81, 0.2);
  }
}

.label {
  font-size: 16px;
  color: #666;
  margin-right: 12px;
}

.input-field {
  flex: 1;
  height: 52px;
  font-size: 16px;
  background: transparent;
}

.placeholder {
  color: #999;
}

.options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding: 0 5px;
}

.remember-pwd {
  display: flex;
  align-items: center;
  
  .checkbox {
    width: 20px;
    height: 20px;
    border-radius: 4px;
    border: 1px solid #ccc;
    margin-right: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    
    &.checked {
      
      border-color: #ff5151;
      
      .checkbox-inner {
        background-color: #fff;
        width: 10px;
        height: 10px;
        border-radius: 2px;
      }
    }
  }
  
  text {
    font-size: 14px;
    color: #666;
  }
}

.checkbox-inner {
  color: #ff5151;
  font-size: 16px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.forgot-pwd {
  font-size: 14px;
  color: #ff5151;
  padding: 5px;
}

.login-btn {
  background: linear-gradient(135deg, #ff5151, #ff8383);
  height: 52px;
  border-radius: 12px;
  font-size: 18px;
  color: #fff;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  border: none;
  letter-spacing: 1px;
}

.button-hover {
  transform: translateY(2px);
  box-shadow: 0 5px 15px rgba(255, 81, 81, 0.3);
  opacity: 0.9;
}

.links {
  display: flex;
  justify-content: center;
  margin-top: 25px;
}

.link-text {
  color: #ff5151;
  font-size: 14px;
  position: relative;
  padding: 5px;
  
  &:after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 100%;
    height: 1px;
    background: #ff5151;
    transform: scaleX(0);
    transition: transform 0.3s ease;
  }
  
  &:hover:after {
    transform: scaleX(1);
  }
}
</style>
